
"""controls the pypi from a remote server to perform maintainance"""

import optparse
from urllib2 import urlopen
import feedparser
from datetime import datetime

def main():
    p = optparse.OptionParser()
    default="http://pypi.appspot.com/p/fetch"
    p.add_option('--gae-fetch', help="URL to pypi on GAE's fetch (default: %s)" % default, default=default)
    default = 'http://pypi.python.org/pypi?%3Aaction=rss'
    p.add_option('--pypi-rss', help="URL to the real PyPi's RSS feed (default: %s)" % default, default=default)
    (options, args) = p.parse_args()
    
    rss = feedparser.parse(options.pypi_rss)
    print "[%s] start" % (datetime.now())
    print rss.feed.title
    print rss.feed.link
    prefix = 'http://pypi.python.org/pypi/'
    for entry in rss.entries:
        # I.E. http://pypi.python.org/pypi/virtualenv/1.1
        if not entry.link.startswith(prefix):
            raise ValueError("Unexpected package link: %s" % entry.link)
        package_path = entry.link[len(prefix):]
        fetch = "%s/%s" % (options.gae_fetch, package_path)
        print "link=", entry.link
        print "fetch=", fetch
        p = urlopen(fetch)
        print p.read()
        p.close()

if __name__ == '__main__':
    main()
